package com.zdoon.controller;

import com.zdoon.pojo.PageResult;
import com.zdoon.pojo.Result;
import com.zdoon.pojo.Student;
import com.zdoon.pojo.StudentQueryParam;
import com.zdoon.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("分页查询学生信息，参数：{}", studentQueryParam);
        PageResult<Student> studentPageResult = studentService.page(studentQueryParam);
        return Result.success(studentPageResult);
    }

    @DeleteMapping("{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学生信息，参数：{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加学生信息，参数：{}", student);
        studentService.add(student);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result get(@PathVariable Integer id){
        log.info("查询学生信息，参数：{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("更新学生信息，参数：{}", student);
        studentService.update(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score){
        log.info("更新学生信息，参数：{},{}", id, score);
        studentService.updateViolation(id, score);
        return Result.success();
    }
}
